<?php
require('../lib/base.php');
$base=new base('web');
$action=$base->get('action');$leave=19;
if($action=='get'){
	$leave=((((int)(($base->runtime+28800)/86400))*86400)-28800)+19*86400;//可售票时间
	$wicket=intval(date('H',$base->runtime));
	if($wicket<8){$wicket=8;}
	$leave=$leave+($wicket+2)*3600;
	$subscribe=$base->model('subscribe');
	$pams['select']='user,password,train,`frae`,`to`,`leave`,`seat`,kind,name,certificate,`number`,mobile,id';
	$pams['where']=array('`leave`<'=>$leave,'locked='=>2);
	//$pams['where']=array('id='=>35);
	$ticket=$subscribe->GSubscribe(array('select'=>$pams['select'].',COUNT(*) AS unite','where'=>$pams['where'],'group'=>array('member','user','password','train','frae','to','leave'),'limit'=>array(0,1)));
	if(empty($ticket)){
		echo 'window.inaoke.message("当前时间未有任何可用数据.");';
	}else{
		$unite=reset($ticket);
		if($unite['unite']>1){
			$pams['where']['user=']=$unite['user'];$pams['where']['password=']=$unite['password'];
			$pams['where']['train=']=$unite['train'];$pams['where']['`frae`=']=$unite['frae'];
			$pams['where']['`to`=']=$unite['to'];$pams['where']['`leave`=']=$unite['leave'];
			$ticket=$subscribe->GSubscribe(array('select'=>$pams['select'],'where'=>$pams['where']));
		}
		if(empty($ticket)){
			echo 'window.inaoke.message("数据获取错误:01.");';
		}else{
			$sort_id=array();$update['set']=array('locked='=>3);
			foreach($ticket as $key=>$value){
				$sort_id[$value['frae']]=$value['frae'];
				$sort_id[$value['to']]=$value['to'];
				$sort_id[$value['seat']]=$value['seat'];
				$sort_id[$value['kind']]=$value['kind'];
				$sort_id[$value['certificate']]=$value['certificate'];
				$update['where']['id IN'][]=$value['id'];
			}
			$affected=$subscribe->USubscribe($update);
			if($affected==count($ticket)){
				$sort=$base->model('sort');
				$sort=$sort->GSort(array('where'=>array('id IN'=>$sort_id)));
				$output='login:{user:"'.$unite['user'].'",password:"'.$unite['password'].'"},';
				$temp=explode('|',$sort[$unite['frae']]['value']);
				$output.='subscribe:{train:"'.$unite['train'].'",frae_char:"'.$temp[0].'",frae:"'.$sort[$unite['frae']]['name'].'",';
				$temp=explode('|',$sort[$unite['to']]['value']);
				$output.='to_char:"'.$temp[0].'",to:"'.$sort[$unite['to']]['name'].'",leave:"'.date('Y-m-d',$unite['leave']).'"},';
				$output.='confirm:{leave:"'.date('Y-m-d',$unite['leave']).'",wicket:'.date('H',$unite['leave']).',purchaser:[';
				$feed='';
				foreach($ticket as $key=>$value){
					$feed.=$value['id'].',';
					$output.='{seat:"'.$sort[$value['seat']]['value'].'",kind:"'.$sort[$value['kind']]['value'].'",';
					$output.='name:"'.$value['name'].'",certificate:"'.$sort[$value['certificate']]['value'].'",number:"'.$value['number'].'",mobile:"'.$value['mobile'].'"},';
				}
				$output=substr_replace($output,'',-1,1);
				$output.=']},';
				$output.='feed:{subscribe:"'.substr_replace($feed,'',-1,1).'"}';
				echo 'window.inaoke.call("storage",\'{'.$output.'}\');';
			}else{
				echo 'window.inaoke.message("数据获取错误:02.");';
			}
		}
	}
}else if($action=='update'){
	$id=$base->get('subscribe');$id=array_map('intval',explode(',',$id));
	$locked=intval($base->get('locked'));
	$pams=array('set'=>array('locked='=>$locked),'where'=>array('id IN'=>$id));
	if($base->get('remark')!==false){
		$pams['set']['remark=']=$base->get('remark',true);
	}
	$subscribe=$base->model('subscribe');
	$affected=$subscribe->USubscribe($pams);
	if($affected>=1&&$locked==0){//操作成功
		$data=$subscribe->GSubscribeFull(array('where'=>array('A.id IN'=>$id)));
		$msg='12306抢票系统下单成功通知：(请45分钟内前往支付)';$send=array('15801005598'=>'15801005598');$pear=array();$leave='';
		foreach($data as $value){
			$send[$value['mobile']]=$value['mobile'];
			$pear[]=$value['name'];
			$leave=date('Y-m-d',$value['leave']);
		}
		$send=implode(',',$send);$msg=$msg.'乘坐时间:'.$leave.',乘坐人:'.implode('、',$pear);
		$base->library('nusoap');
		$soapclient=new soapclient('http://mobile.bitauto.com/smsplatform/interface/MsgService.asmx?WSDL',true);
		$soapclient->soap_defencoding='utf-8';
		$soapclient->decode_utf8=false;
		$soapclient->xml_encoding='utf-8';
		$result=$soapclient->call('SendMsgImmediately',array(array('productcode'=>'6124','phonenums'=>$send,'content'=>$msg,'prefix'=>'','endtime'=>'23:59:59','md5'=>md5('6124'.$send.$msg.''.'Oot5xyY/1pfvaOCSRVhEjbCTjaJztcZvKmJ9dNbEgYM='))));
		//$Service=new SoapClient('http://mobile.bitauto.com/smsplatform/interface/MsgService.asmx?WSDL');
		//$result=$Service->__soapCall('SendMsgImmediately',array(array('productcode'=>'6124','phonenums'=>$send,'content'=>$msg,'prefix'=>'','endtime'=>'23:59:59','md5'=>md5('6124'.$send.$msg.''.'Oot5xyY/1pfvaOCSRVhEjbCTjaJztcZvKmJ9dNbEgYM='))));
	}else{

	}
}else if($action=='MMSTR'){
	$date=$base->get('date',true);$train=$base->get('train',true);
	if(empty($date)||empty($train)){
		//$leave=((((int)(($base->runtime+28800)/86400))*86400)-28800)+$leave*86400;
		$leave=((((int)(($base->runtime+28800)/86400))*86400)-28800)+15*86400;
		$subscribe=$base->model('subscribe');
		$T=$subscribe->GSubscribe(array('select'=>'`frae`,`to`,`leave`,train','where'=>array('`leave`>='=>$leave),'group'=>array('leave','train')));
		if(empty($T)){
			echo 'window.inaoke.message("无预定.");';
		}else{
			$sort_id=array();
			foreach($T as $key=>$value){
				$sort_id[$value['frae']]=$value['frae'];
				$sort_id[$value['to']]=$value['to'];
			}
			$sort=$base->model('sort');
			$sort=$sort->GSort(array('where'=>array('id IN'=>$sort_id)));
			$mm='';
			foreach($T as $value){
				$frae=explode('|',$sort[$value['frae']]['value']);$to=explode('|',$sort[$value['to']]['value']);
				$value['leave']=date('Y-m-d',intval($value['leave']));
				$ticket[$value['leave'].$value['train'].$frae[0].$to[0]]='{frae:"'.$frae[0].'",to:"'.$to[0].'",date:"'.$value['leave'].'",train:"'.$value['train'].'"}';
				$mm.="(date='{$value['leave']}' AND train='{$value['train']}') OR ";
			}
			$mm=rtrim($mm,' OR ');
			$mmstr=$base->model('mmstr');
			$T=$mmstr->GMmstr(array('where'=>$mm));
			foreach($T as $value){
				if(isset($ticket[$value['date'].$value['train'].$value['frae'].$value['to']])){
					unset($ticket[$value['date'].$value['train'].$value['frae'].$value['to']]);
				}
			}
			$ticket=array_values($ticket);
			if(empty($ticket)){
				echo 'window.inaoke.message("所有数据已获取完毕.");';
			}else{
				$ticket=implode(',',$ticket);
				echo 'window.inaoke.mmstr(\'['.$ticket.']\');';
			}
		}
	}else{
		$frae=$base->get('frae',true);$to=$base->get('to',true);
		$mmstr=$base->model('mmstr');
		$mmstr=reset($mmstr->GMmstr(array('where'=>array('date='=>$date,'train='=>$train,'`frae`='=>$frae,'`to`='=>$to))));
		if(empty($mmstr)){
			echo 'window.inaoke.auto("")';
		}else{
			echo 'window.inaoke.auto(\'{date:"'.$mmstr['date'].'",mmstr:"'.$mmstr['mmstr'].'"}\')';
		}
	}
}else if($action=='CODE'){
	$date=$base->get('date',true);$train=trim($base->get('train',true));
	$frae=$base->get('frae',true);$to=$base->get('to',true);$code=trim($base->get('mmstr',true));
	$mmstr=$base->model('mmstr');
	$T=$mmstr->GMmstr(array('where'=>array('date='=>$date,'train='=>$train,'`frae`='=>$frae,'`to`='=>$to)));
	if(empty($T)){
		$mmstr->IMmstr(array('date'=>$date,'train'=>$train,'frae'=>$frae,'to'=>$to,'mmstr'=>$code));
	}else{
		$mmstr->UMmstr(array('where'=>array('date='=>$date,'train='=>$train,'`frae`='=>$frae,'`to`='=>$to),'set'=>array('mmstr='=>$code)));
	}
}
/*
$mmstr=$base->model('mmstr');
$T=$mmstr->GMmstr();
foreach($T as $value){
	$mmstr->UMmstr(array('where'=>array('date='=>$value['date'],'train='=>$value['train']),'set'=>array('date='=>date('Y-m-d',intval($value['date'])))));
}*/
?>